package com.youkeda.yikao.web.control;

import com.google.code.kaptcha.Constants;
import com.youkeda.yikao.web.dao.ChenzikangUesrSystemMapper;
import com.youkeda.yikao.web.dataobject.ChenzikangUesrSystem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.util.DigestUtils;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * @author ：chenzikang
 * @date ： 2019/9/5 19:24
 */

@Controller
public class SignupControl {


    @Autowired
    ChenzikangUesrSystemMapper chenzikangUesrSystemMapper;



    /**
     *
     * 打开注册页面
     */
    @RequestMapping(path = "signup")
    public ModelAndView toSignup(@ModelAttribute("userForm") ChenzikangUesrSystem chenzikangUesrSystem,
                                 Model model) {
        ModelAndView view = new ModelAndView();
        view.addObject(BindingResult.class.getName()+".userForm"
                ,model.asMap().get("userFormError"));
        view.setViewName("signup");
        return view;
    }


    /**
     *
     * 跳转到注册成功页
     */
    @RequestMapping(path = "signSuccess")
    public ModelAndView signSuccess(@ModelAttribute("userForm3") ChenzikangUesrSystem chenzikangUesrSystem
                                    ){
        ModelAndView view = new ModelAndView();
        view.setViewName("signSuccess");
        return view;
    }

    /**
     *
     * 注册的验证
     */
    @RequestMapping(path = "/chenzikangUserSystem/signup")
    public RedirectView signup(@Valid ChenzikangUesrSystem chenzikangUesrSystem,
                               BindingResult result,
                               HttpSession session,
                               RedirectAttributes attributes) {
        RedirectView view = new RedirectView();

//        验证是否为空
        if (result.hasErrors()) {
            attributes.addFlashAttribute("userForm", chenzikangUesrSystem);
            attributes.addFlashAttribute("userFormError",result);

            view.setUrl("/signup");
        }else {

//            确定验证码是否正确
            if (!chenzikangUesrSystem.getCaptcha().equals(session.getAttribute(Constants.KAPTCHA_SESSION_KEY))) {
                //将内容回填
                attributes.addFlashAttribute("userForm", chenzikangUesrSystem);
                view.setUrl("/signup");
            }else {



//                确定邮箱验证码是否正确
                if (!chenzikangUesrSystem.getEmailCode().equals(session.getAttribute("emailCode"))) {

                    attributes.addFlashAttribute("userForm", chenzikangUesrSystem);
                    view.setUrl("/signup");
                } else {
                    session.removeAttribute("emailCode");

                    //md5加密
                    String md5Password = DigestUtils.md5DigestAsHex(chenzikangUesrSystem.getPwd().getBytes());


                    //写入数据
                    ChenzikangUesrSystem uesrSystem = new ChenzikangUesrSystem();
                    uesrSystem.setUser(chenzikangUesrSystem.getUser());
                    uesrSystem.setPwd(md5Password);
                    uesrSystem.setPrefix(chenzikangUesrSystem.getPrefix());
                    uesrSystem.setName(chenzikangUesrSystem.getName());

                    chenzikangUesrSystemMapper.insert(uesrSystem);
                    view.setUrl("/signSuccess");
                }


            }

        }


        return view;

    }

}
